package com.shujia;


import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.HashSet;
import java.util.Properties;
import java.util.Scanner;

public class Code01Catch {
    public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
        /*
            实现如下功能:
                通过输入用户名和密码对 Mysql数据库中的数据进行做查询，并将结果进行返回，同时再整个架构中插入Redis做数据的缓存
                    实现效果为：
                        1. 先从Redis中查询对应的数据，如果有数据，那么直接返回
                        2. 如果没有数据，那么需要从Mysql中进行数据查询，并将结果返回，同时将数据保存至Redis中

         */

        // 解决输入问题：
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎登录大数据26期集训营");
        System.out.println("=====================");
        System.out.println("请输入用户名:");
        String user = "";
        if (scanner.hasNext()) {
            user = scanner.next();
        }
        System.out.println("请输入密码:");
        String passwd = "";
        if (scanner.hasNext()) {
            passwd = scanner.next();
        }

        // 加载配置对象
        Properties properties = new Properties();
        properties.load(new FileReader("redisCode/src/main/resources/mysql.properties"));


        // 创建Mysql连接对象
        Class.forName("com.mysql.jdbc.Driver");
        String mysqlUser = properties.getProperty("user");
        String mysqlPasswd = properties.getProperty("passwd");
        String url = properties.getProperty("url");


        HashSet<HostAndPort> hostAndPorts = new HashSet<>();
        hostAndPorts.add(new HostAndPort("192.168.163.100", 6380));
        hostAndPorts.add(new HostAndPort("192.168.163.100", 6381));
        hostAndPorts.add(new HostAndPort("192.168.163.100", 6382));
        // 创建Jedis对象
        JedisCluster jedisCluster = new JedisCluster(hostAndPorts);


        Connection connection = DriverManager.getConnection(url, mysqlUser, mysqlPasswd);
        String findPasswd = findDataFromMysql(jedisCluster,connection, user);

        if (findPasswd.equals(passwd)) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }



        // 关闭资源
        connection.close();
        jedisCluster.close();


    }


    public static String findDataFromMysql(JedisCluster jedis,Connection mysqlCon, String user) throws SQLException {
        PreparedStatement preparedStatement = mysqlCon.prepareStatement("select * from bigdata_user where user =?");

        preparedStatement.setString(1, user);

        ResultSet resultSet = preparedStatement.executeQuery();

        String mysqlPs = null;
        if (resultSet.next()) {
            System.out.println("Mysql查询到数据");
            mysqlPs = resultSet.getString("passwd");
            // 当Mysql中查询到数据后，需要将数据缓存至Redis中
            insertDataToRedis(jedis,user,mysqlPs);
        }
        return mysqlPs;
    }


    public static void insertDataToRedis(JedisCluster jedis, String key, String value){
        jedis.set(key,value);
        System.out.println("用户信息："+key+"已经被插入到Redis中");
    }
}
